//
//  MOOfficialListViewController.m
//  MobileOffice
//
//  Created by 晓 on 2019/6/10.
//  Copyright © 2019 com.idolmedia. All rights reserved.
//

#import "MOOfficialListViewController.h"
#import "MOCommonUseViewCell.h"
#import "MOOfficeTextSearchViewController.h"
#import "MONoticeViewController.h"

@interface MOOfficialListViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic,strong)UICollectionView *collectionView;

@property(nonatomic,strong)NSMutableArray *dataArray;

@end

@implementation MOOfficialListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initView];
    
    [self requestData];
    
    [self GWLogin];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(GWLogin) name:@"officeTextStatusChange" object:nil];
}

- (void)initView{
    self.title = @"公文";
    [self.view addSubview:self.collectionView];
}

-(void)requestData
{
    self.dataArray = [NSMutableArray array];
    NSArray *nameArr = @[@"待办公文",@"已办公文",@"公文查询",@"局内发布"];
    NSArray *imageArr = @[@"use_daiban",@"use_daibanO",@"use_chaxun",@"use_jiandingwenan"];
    for (int i = 0; i<nameArr.count; i++) {
        MOCommonUseModel *model = [[MOCommonUseModel alloc]init];
        model.nameStr = nameArr[i];
        model.imageStr = imageArr[i];
        model.itemIndex = i;
        model.typeStr = @"3";
//        model.countStr = @"99";
        [self.dataArray addObject:model];
    }
    [self.collectionView reloadData];
}

-(void)GWLogin
{
    NSString *postUrl = [NSString stringWithFormat:@"%@?authorization=%@",ODPSCX_LOGIN_URL,[NSString URLEncodedString:[MOUser sharedMOUser].token]];
    
    [PPNetworkHelper GET:postUrl parameters:@{} success:^(id responseObject) {
        
        NSLog(@"公文系统登录:%@",responseObject);
        if ([responseObject[@"code"] integerValue]==0) {//成功
            for (int i = 0; i<self.dataArray.count; i++) {
                MOCommonUseModel *model = self.dataArray[i];
                if (i==0&&[responseObject[@"totalCount"] integerValue]>0) {
                    if ([responseObject[@"totalCount"] integerValue]>99) {
                        model.countStr = @"99+";
                    }else{
                        model.countStr = [NSString passNullForString:responseObject[@"totalCount"]];
                    }
                }
            }
            [self.collectionView reloadData];
            
            UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
            [button setTitle:[NSString stringWithFormat:@" %@",responseObject[@"userName"]] forState:UIControlStateNormal];
            [button setImage:IMAGE(@"setUp_userInfo") forState:UIControlStateNormal];
            [button setTitleColor:KBlueColor_2C94FF forState:UIControlStateNormal];
            button.titleLabel.font = FONT_SIZE(14);
            
            UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:button];
            self.navigationItem.rightBarButtonItem = rightItem;
            
        }else{
            [[ADShowMessage shareShowMessage] showTextOnly:responseObject[@"msg"] showInView:self.view];
        }
        
    } failure:^(NSError *error) {
        
        [[ADShowMessage shareShowMessage] showTextOnly:[error localizedDescription] showInView:self.view];
    }];
}

#pragma mark -UICollectionViewDataSource,UICollectionViewDelegate
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    MOCommonUseViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.model = self.dataArray[indexPath.item];
    return cell;
    
}
//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake(SCREEN_WIDTH/3, SCREEN_WIDTH/3);
    
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    
    return UIEdgeInsetsMake(0, 0, 0, 0);
    
}

//定义每个UICollectionView 横向的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    
    return 0;
}

//定义每个UICollectionView 竖向的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    
    return 0;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    return CGSizeMake(0,0);
    
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.item==0) {
        MONoticeViewController *vc = [[MONoticeViewController alloc]init];
        vc.typeStr = @"1";
        [self.navigationController pushViewController:vc animated:YES];
    }else if (indexPath.item==1){
        MONoticeViewController *vc = [[MONoticeViewController alloc]init];
        vc.typeStr = @"2";
        [self.navigationController pushViewController:vc animated:YES];
    }else if (indexPath.item==2){
        MOOfficeTextSearchViewController *vc = [[MOOfficeTextSearchViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }else if (indexPath.item==3){
        MONoticeViewController *vc = [[MONoticeViewController alloc]init];
        vc.typeStr = @"4";
        [self.navigationController pushViewController:vc animated:YES];
    }
}


#pragma mark -懒加载
-(UICollectionView *)collectionView
{
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT-StatusBarAndNavigationBarHeight) collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.alwaysBounceVertical = YES;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        
        [_collectionView registerClass:[MOCommonUseViewCell class] forCellWithReuseIdentifier:@"cell"];
        
    }
    return _collectionView;
}

@end
